package com.dd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDto;
import com.dd.reggie.common.CustomException;
import com.dd.reggie.common.R;
import com.dd.reggie.dto.SetmealDto;
import com.dd.reggie.entity.Category;
import com.dd.reggie.entity.Setmeal;
import com.dd.reggie.entity.SetmealDish;
import com.dd.reggie.service.CategoryService;
import com.dd.reggie.service.SetmealDishService;
import com.dd.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){

        log.info(setmealDto.toString());

        setmealService.saveWithSetmeal(setmealDto);

        return R.success("新增成功");
    }
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){

        Page<Setmeal> pageInfo = new Page<>( page, pageSize);

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Setmeal::getName, name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo, queryWrapper);

        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> setmealDtos = records.stream().map((item) -> {

            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);

            String categoryName = categoryService.getById(item.getCategoryId()).getName();

            setmealDto.setCategoryName(categoryName);

            return setmealDto;
        }).collect(Collectors.toList());

        Page<SetmealDto> setmealDtoPage = new Page<>();

        setmealDtoPage.setRecords(setmealDtos);

        return R.success(setmealDtoPage);
    }

    @GetMapping("/{id}")
    public R<SetmealDto> list(@PathVariable Long id){

        //查询套餐信息
        Setmeal setmeal = setmealService.getById(id);
        String categoryName = categoryService.getById(setmeal.getCategoryId()).getName();
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        setmealDto.setCategoryName(categoryName);

        //查询套餐包含菜品信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmeal.getId());

        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        setmealDto.setSetmealDishes(list);

        return R.success(setmealDto);
    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){

        setmealService.updateWithSetmeal(setmealDto);

        return R.success("修改成功");
    }

    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable int status, @RequestParam List<Long> ids){

        //select count(*) from setmeal where id in ids and Status = status
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId, ids).eq(Setmeal::getStatus, status);
        int count = setmealService.count(queryWrapper);

        if(count > 0){
            String msg = new String();
            if(status == 1){
                msg = "所选套餐中存在状态为启售状态的菜品";
            }
            else{
                msg = "所选套餐中存在状态为停售状态的菜品";
            }
            throw new CustomException(msg);
        }

        for(Long id : ids){
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }

        return R.success("状态修改成功");
    }

    @DeleteMapping()
    public R<String> delete(@RequestParam List<Long> ids){

        setmealService.removeWithSetmeal(ids);

        return R.success("删除成功");
    }

    @GetMapping("/list")
    public R<List<Setmeal>> list(Long categoryId, int status){

        log.info(categoryId.toString());

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId, categoryId).eq(Setmeal::getStatus, status);
        List<Setmeal> list = setmealService.list(queryWrapper);

        return R.success(list);
    }

}
